/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.FinalUser;

/**
 *
 * @author Edison Parra
 * @version 1.0 
 */
public class UserDao {

    /**Search all Users.
     * @param em EntityManager object to make the operation
     * @return All registered users
     */
    public List<FinalUser> searchAll(final EntityManager em) {
        Query q = em.createNamedQuery("FinalUser.findAll");
        return (List<FinalUser>) q.getResultList();
    }

    /**Search by the username.
     * @param un Username given by the user
     * @param em EntityManager object to make the operation
     * @return The user with the information related to the username given
     */
    public List<FinalUser> searchByUsername(final String un, final EntityManager em) {
        Query q = em.createNamedQuery("FinalUser.findByUserName");
        q.setParameter("userName", un);
        List<FinalUser> user = new ArrayList<FinalUser>();
        user.add((FinalUser) q.getSingleResult());
        return user;
    }

    /**Search by the identification.
     * @param n Identification number given by the user
     * @param em EntityManager object to make the operation
     * @return The userVO with the information related to the identification
     * number given
     */
    public List<FinalUser> searchByIdentification(final String n, final EntityManager em) {
        Query q = em.createNamedQuery("FinalUser.findByDocument").setParameter("document", n);
        List<FinalUser> user = new ArrayList<FinalUser>();
        user.add((FinalUser) q.getSingleResult());
        return user;
    }

    /**Search by the role.
     * @param rId Id from the role given by the user like search criteria
     * @param em EntityManager object to make the operation
     * @return The userVOs the role given
     */
    public List<FinalUser> searchByRole(final long rId, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM FinalUser u WHERE u.rol.id = :rId").setParameter("rId", 1);
        List<FinalUser> users = (List<FinalUser>) q.getResultList();
        return users;
    }

    /**Search by username and document.
     * @param un Username given by the user
     * @param doc Document given by the user
     * @param em EntityManager object to make the operation
     * @return The users with the parameters given
     */
    public List<FinalUser> searchByUserAndDoc(final String un, final String doc, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM FinalUser u WHERE u.userName = :un AND u.document = :doc").setParameter("un", un).setParameter("doc", doc);
        List<FinalUser> users = (List<FinalUser>) q.getResultList();
        return users;
    }

    /**Search by username and role.
     * @param un Username given by the user
     * @param rId Role Id given by the user
     * @param em EntityManager object to make the operation
     * @return The users with the parameters given
     */
    public List<FinalUser> searchByUserAndRol(final String un, final long rId, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM FinalUser u WHERE u.userName = :un AND u.rol.id = :rId").setParameter("un", un).setParameter("rId", rId);
        List<FinalUser> users = (List<FinalUser>) q.getResultList();
        return users;
    }

    /**Search by document and role.
     * @param doc Document given by the user
     * @param rId Role Id given by the user
     * @param em EntityManager object to make the operation
     * @return The users with the parameters given
     */
    public List<FinalUser> searchByDocAndRol(final String doc, final long rId, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM FinalUser u WHERE u.document = :doc AND u.rol.id = :rId").setParameter("doc", doc).setParameter("rId", rId);
        List<FinalUser> users = (List<FinalUser>) q.getResultList();
        return users;
    }

    /**Search by username, document and role.
     * @param un Username given by the user
     * @param doc Document given by the user
     * @param rId Role Id given by the user
     * @param em EntityManager object to make the operation
     * @return The users with the parameters given
     */
    public List<FinalUser> searchByUserAndDocAndRol(final String un, final String doc, final long rId, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM FinalUser u WHERE u.userName = :un AND u.document = :doc AND u.rol.id = :rId")
                .setParameter("un", un)
                .setParameter("doc", doc)
                .setParameter("rId", rId);
        List<FinalUser> users = (List<FinalUser>) q.getResultList();
        return users;
    }

    /**Update an user.
     * @param u The user object that will be updated
     * @param em EntityManager object to make the operation
     */
    public void update(final FinalUser u, final EntityManager em) {
        em.merge(u);
    }

    /**Delete an user.
     * @param u The user object that will be deleted
     * @param em EntityManager object to make the operation
     */
    public void delete(final FinalUser u, final EntityManager em) {
        em.remove(em.merge(u));
    }

    /**Create an user.
     * @param u The user object that will be created
     * @param em EntityManager object to make the operation
     */
    public void create(final FinalUser u, final EntityManager em) {
        em.persist(u);
    }
}
